<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" label-width="68px">
      <el-form-item label="账号" prop="username">
        <el-input
          v-model="queryParams.username"
          placeholder="请输入账号"
          clearable
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号码" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号码"
          clearable
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="昵称" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入昵称"
          clearable
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-button type="primary" v-checkPermission="'admin:user:add'">新增</el-button>
    <el-button type="danger" v-checkPermission="'admin:user:delete'">删除</el-button>

    <el-table v-loading="loading" :data="list">
      <el-table-column label="用户编号" prop="id" align="center" />
      <el-table-column label="账号" prop="username" align="center" />
      <el-table-column label="昵称" prop="nickname" align="center" />
      <el-table-column label="邮箱" prop="email" align="center" />
      <el-table-column label="手机号" prop="phone" align="center" />
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="onGetList"
    />
  </div>
</template>
<script setup lang="ts">
import type { ElForm } from 'element-plus'
import { getList, type User } from '@/views/system/api/user'

const loading = ref(true)
const total = ref(0)
const list = ref<User[]>([])

onMounted(() => {
  onGetList()
})
/** 查询用户列表 */
const onGetList = async () => {
  loading.value = true

  try {
    const res = await getList()
    list.value = res.data.list
    total.value = res.data.total
  } catch (error) {
    console.log(error)
  }

  loading.value = false
}

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    username: undefined,
    phone: undefined,
    nickName: undefined,
  },
})
const { queryParams } = toRefs(data)

// 表单重置
const resetForm = () => {
  data.queryParams = {
    pageNum: 1,
    pageSize: 10,
    username: undefined,
    phone: undefined,
    nickName: undefined,
  }
}
const queryRef = ref<InstanceType<typeof ElForm>>()

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1
  onGetList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  resetForm()
  queryRef.value?.resetFields()
  handleQuery()
}
</script>

<script lang="ts">
import type { RouteMeta } from 'vue-router'
import { el } from 'element-plus/es/locales.mjs'
export const meta: RouteMeta = {
  title: '用户', // 标题
  name: 'system-user-list', // 路由名称
  icon: 'User', // 图标
  isHide: false, // 代表路由在菜单中是否隐藏，是否隐藏
  isKeepAlive: true, // 是否缓存路由数据
  isTag: true,
  permission: 'admin:user:list',
}
</script>
